$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        um: null,
        init: function () {
            if (ifAnswer == 1) {
                this.funObj().newEditor('sContent');
            }
            this.listener();
        },
        listener: function () {
            var $this = this;
            //回复
            $('.answer-bar').click(function () {
                $('.answerContent').slideToggle('slow');
            });
            //提交答案
            $('.submit-btn').click(function () {
                var answerId = $(this).attr('data-id');
                if (answerId > 0) {
                    $this.funObj().editAnswer(answerId);
                } else {
                    $this.funObj().addAnswer();
                }
            });
            //删除回复
            $('.del-myAnswer').click(function () {
                var answerId = $(this).attr('data-id');
                $this.funObj().delAnswer(answerId);
            });
            //编辑回复
            $('.edit-myAnswer').click(function () {
                $('.edit-myAnswer').hide();
                $('.edit-cancle').show();
                $('.edit-cancle').css('margin-right', '10px');
                $('.answerContent').slideToggle('slow');
                var content = $(this).parent().prev().children('.con').html();
                $this.funObj().newEditor('sContent');
                $this.um.addListener("ready", function () {
                    $this.um.setContent(content);
                });
            });
            //取消编辑
            $('.edit-cancle').click(function () {
                $this.um = null;
                $('.edit-myAnswer').show();
                $('.edit-cancle').hide();
                $('.edit-cancle').css('margin-right', '10px');
                $('.answerContent').slideToggle('slow');
            });

            //采纳回复
            $('.adopt-answer').click(function () {
                var answerId = $(this).attr('data-id');
                $this.funObj().adoptAnswer(answerId);
            });
        },
        funObj: function () {
            var $this = this;
            return {
                addAnswer: function () {
                    var iProblemId = $('#iProblemId').val();
                    var sContent = $this.um.getContent();
                    var url = 'webproblem/saveAnswer';
                    xhExtra.ajaxRequest(url, {iProblemId: iProblemId, sContent: sContent}, {}, function (data) {
                        if (xhExtra.checkError(data)) {
                            return false;
                        } else {
                            window.top.xhTop.showSuccess(data.msg);
                            location.reload();
                        }
                    });
                },
                editAnswer: function (id) {
                    var iProblemId = $('#iProblemId').val();
                    var sContent = $this.um.getContent();
                    var url = 'webproblem/saveAnswer';
                    xhExtra.ajaxRequest(url, {
                        iProblemId: iProblemId,
                        sContent: sContent,
                        iId: id
                    }, {}, function (data) {
                        if (xhExtra.checkError(data)) {
                            return false;
                        } else {
                            window.top.xhTop.showSuccess(data.msg);
                            location.reload();
                        }
                    });
                },
                delAnswer: function (id) {
                    var url = 'webproblem/delAnswerById';
                    xhExtra.ajaxRequest(url, {iId: id}, {}, function (data) {
                        if (xhExtra.checkError(data)) {
                            return false;
                        } else {
                            window.top.xhTop.showSuccess(data.msg);
                            location.reload();
                        }
                    });
                },
                adoptAnswer: function (id) {
                    var url = 'webproblem/adoptAnswerById';
                    xhExtra.ajaxRequest(url, {iId: id}, {}, function (data) {
                        if (xhExtra.checkError(data)) {
                            return false;
                        } else {
                            window.top.xhTop.showSuccess(data.msg);
                            location.reload();
                        }
                    });
                },

                newEditor: function (id) {
                    $this.um = UE.getEditor(id, {
                        autoHeightEnabled: true,
                        serverUrl: baseUrl + 'webproblem/uploadImage',
                        textarea: 'sContent',
                        initialFrameWidth: '100%',
                        imageScaleEnabled: false,
                        toolbars: [[
                            'source', '| ', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'strikethrough', '|', 'superscript', 'subscript', '|',
                            'forecolor', 'backcolor', '|', 'removeformat',
                            '|', 'insertorderedlist', 'insertunorderedlist', '|', 'selectall', 'cleardoc', 'paragraph',
                            '|', 'justifyleft', 'justifycenter', 'justifyright',
                            '|', 'insertimage', 'insertvideo', 'link', 'unlink',
                            '|', 'horizontal', 'preview ',
                        ]],

                        /* 上传图片配置项 */
                        "imageActionName": "uploadImage", /* 执行上传图片的action名称 */
                        "imageFieldName": "upfile", /* 提交的图片表单名称 */
                        "imageMaxSize": 2048000, /* 上传大小限制，单位B */
                        "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
                        "imageCompressEnable": true, /* 是否压缩图片,默认是true */
                        "imageCompressBorder": 1600, /* 图片压缩最长边限制 */
                        "imageInsertAlign": "none", /* 插入的图片浮动方式 */
                        "imageUrlPrefix": "", /* 图片访问路径前缀 */
                        "imagePathFormat": "",

                        /* 上传视频配置 */
                        "videoActionName": "uploadImage", /* 执行上传视频的action名称 */
                        "videoFieldName": "upfile", /* 提交的视频表单名称 */
                        "videoPathFormat": "", /* 上传保存路径,可以自定义保存路径和文件名格式 */
                        "videoUrlPrefix": "", /* 视频访问路径前缀 */
                        "videoMaxSize": 102400000, /* 上传大小限制，单位B，默认100MB */
                        "videoAllowFiles": [
                            ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
                            ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], /* 上传视频格式显示 */
                    });
                }
            }
        }
    }
};

